<template>
    <div class="loginContainer">
        <!--<head-top :head-title="loginWay? '登录':'密码登录'" goBack="true">
           &lt;!&ndash; <div slot="changeLogin" class="change_login" @click="changeLoginWay">{{loginWay? "密码登录":"短信登录"}}</div>&ndash;&gt;
        </head-top>-->
        <header class="login_head">
          <figure class="login_head_img">
            <img src="../../images/cr-036.png"/>
          </figure>
        </header>
        <form class="loginForm" v-if="loginWay">
            <section class="input_container phone_number">
                <img class="phone_number-img" src="../../images/cr-034.png">
                <input type="text" placeholder="手机号" name="phone" maxlength="11" v-model="phoneNumber">
            </section>
            <section class="input_container phone_number">
                <img class="phone_number-img" src="../../images/cr-035.png">
                <input type="text" placeholder="请输入验证码" name="mobileCode" maxlength="6" v-model="mobileCode">
                <button @click.prevent="getVerifyCode" :class="{right_phone_number:rightPhoneNumber}" v-show="!computedTime">获取验证码</button>
                <button @click.prevent v-show="computedTime">已发送({{computedTime}}s)</button>
            </section>
        </form>
        <form class="loginForm" v-else>
            <section class="input_container">
                <input type="text" placeholder="手机号/邮箱/用户名" v-model.lazy="userAccount">
            </section>
            <section class="input_container">
                <input v-if="!showPassword" type="password" placeholder="密码"  v-model="passWord">
                <input v-else type="text" placeholder="密码"  v-model="passWord">
                <div class="button_switch" :class="{change_to_text: showPassword}">
                    <div class="circel_button" :class="{trans_to_right: showPassword}" @click="changePassWordType"></div>
                    <span>abc</span>
                    <span>...</span>
                </div>
            </section>
            <section class="input_container captcha_code_container">
                <input type="text" placeholder="验证码" maxlength="4" v-model="codeNumber">
                <div class="img_change_img">
                    <img v-show="captchaCodeImg" :src="captchaCodeImg">
                    <div class="change_img" @click="getCaptchaCode">
                        <p>看不清</p>
                        <p>换一张</p>
                    </div>
                </div>
            </section>
        </form>
       <!-- <p class="login_tips">
            温馨提示：未注册饿了么账号的手机号，登录时将自动注册，且代表您已同意
            <a href="https://h5.ele.me/service/agreement/">《用户服务协议》</a>
        </p>-->
        <div class="login_container" @click="mobileLogin">登录</div>
        <router-link to="/forget" class="to_forget" v-if="!loginWay">忘记密码？</router-link>
        <alert-tip v-if="showAlert" :showHide="showAlert" @closeTip="closeTip" :alertText="alertText"></alert-tip>
    </div>
</template>

<script src="../common/js/fetch.js"></script>
<script>
    import headTop from '../../components/header/head'
    import alertTip from '../../components/common/alertTip'
    import {mapState, mapMutations} from 'vuex'
    import {getVerifyCode,mobileLogin} from '../../service/getData'
    import {getStore, setStore} from 'src/config/mUtils'

    export default {
        data(){
            return {
                loginWay: true, //登录方式，默认短信登录
                showPassword: false, // 是否显示密码
                phoneNumber: null, //电话号码
                mobileCode: null, //短信验证码
                verifyId: null, //短信验证码
                validate_token: null, //获取短信时返回的验证值，登录时需要
                computedTime: 0, //倒数记时
                userInfo: null, //获取到的用户信息
                userAccount: null, //用户名
                passWord: null, //密码
                captchaCodeImg: null, //验证码地址
                codeNumber: null, //验证码
                showAlert: false, //显示提示组件
                alertText: null, //提示的内容
              deviceId:null
            }
        },
        created(){
            //this.getCaptchaCode();
          this.deviceId = getStore('_bbid');
        },
        components: {
            headTop,
            alertTip,
        },
        computed: {
            //判断手机号码
            rightPhoneNumber: function (){
                return /^1\d{10}$/gi.test(this.phoneNumber)
            }
        },
        methods: {
            ...mapMutations([
                'RECORD_USERINFO',
            ]),
            //改变登录方式
            changeLoginWay(){
                this.loginWay = !this.loginWay;
            },
            //是否显示密码
            changePassWordType(){
                this.showPassword = !this.showPassword;
            },
            //获取验证吗，线上环境使用固定的图片，生产环境使用真实的验证码
            /*async getCaptchaCode(){
                if (process.env.NODE_ENV !== 'development'){
                    this.captchaCodeImg = 'http://test.fe.ptdev.cn/elm/yzm.jpg';
                }else{
                    let res = await getcaptchas();
                    this.captchaCodeImg = 'https://mainsite-restapi.ele.me/v1/captchas/' + res.code;
                }
            },*/
            //获取短信验证码
            async getVerifyCode(){
              var _self = this;
              //var url = "http://10.99.8.51:8080/flowcenter-activity/v9/weibo/bind/verifyMobile?mobile="+this.phoneNumber+"&appCode=other_browser";
              var url = "http://hd.zt.raiyi.com/v9/weibo/bind/verifyMobile?mobile="+this.phoneNumber+"&appCode=other_browser";


              if (this.rightPhoneNumber) {
                    this.computedTime = 30;
                    this.timer = setInterval(() => {
                        this.computedTime --;
                        if (this.computedTime == 0) {
                            clearInterval(this.timer)
                        }
                    }, 1000)
                getVerifyCode(this.phoneNumber,'other_browser').then(json=>{
                  console.log(json);
                  if (json.success) {
                    //_self.showAlert = true;
                    //_self.alertText = json.data;
                    this.verifyId = json.data
                  }
                });
                  /*fetch(url).then(function (response) {
                    return response.json();
                  }).then(function (json) {

                  });*/
                    //判读用户是否存在
                   /* let exsis = await checkExsis(this.phoneNumber, 'mobile');
                    if (exsis.message) {
                        this.showAlert = true;
                        this.alertText = exsis.message;
                        return
                    }else if(!exsis.is_exists) {
                        this.showAlert = true;
                        this.alertText = '您输入的手机号尚未绑定';
                        return
                    }*/
                    //发送短信验证码
                    /*let res = await mobileCode(this.phoneNumber);
                    if (res.message) {
                        this.showAlert = true;
                        this.alertText = res.message;
                        return
                    }
                    this.validate_token = res.validate_token;*/
                }
            },
            //发送登录信息
            async mobileLogin(){
                var _self = this;

                /*var url = "http://10.99.8.51:8080/flowcenter-activity/v9/weibo/bind/verifyCode?mobile="+this.phoneNumber
                  +"&verifyCode="+this.mobileCode+"&appCode=other_browser&verifyId="+this.verifyId; */
                var url = "http://hd.zt.raiyi.com/v9/weibo/bind/verifyCode?mobile="+this.phoneNumber
                  +"&verifyCode="+this.mobileCode+"&appCode=other_browser&verifyId="+this.verifyId;


                if (this.loginWay) {
                    if (!this.rightPhoneNumber) {
                        this.showAlert = true;
                        this.alertText = '手机号码不正确';
                        return
                    }else if(!(/^\d{4}$/gi.test(this.mobileCode))){
                        this.showAlert = true;
                        this.alertText = '短信验证码不正确';
                        return
                    }
                    //手机号登录
                    //this.userInfo = await sendLogin(this.mobileCode, this.phoneNumber, this.validate_token);
                  await mobileLogin(this.deviceId,this.phoneNumber,this.mobileCode,'other_browser',this.verifyId).then(json=>{
                    console.log(json);
                    if(json.success){
                      localStorage.setItem("_uuid",json.data);
                      this.$router.push({path:'/account'});
                    }
                  });
                 /* fetch(url)
                    .then(function (response) {
                      return response.json();
                    }).then(function (json) {

                  });*/
                }else{
                    if (!this.userAccount) {
                        this.showAlert = true;
                        this.alertText = '请输入手机号/邮箱/用户名';
                        return
                    }else if(!this.passWord){
                        this.showAlert = true;
                        this.alertText = '请输入密码';
                        return
                    }else if(!this.codeNumber){
                        this.showAlert = true;
                        this.alertText = '请输入验证码';
                        return
                    }
                    //用户名登录
                    this.userInfo = await accountLogin(this.userAccount, this.passWord, this.codeNumber);
                }
                //如果返回的值不正确，则弹出提示框，返回的值正确则返回上一页
               /* if (!this.userInfo.user_id) {
                    this.showAlert = true;
                    this.alertText = this.userInfo.message;
                    if (!this.loginWay) this.getCaptchaCode();
                }else{
                    this.RECORD_USERINFO(this.userInfo);
                    this.$router.go(-1);

                }*/
            },
            closeTip(){
                this.showAlert = false;
            }
        }
    }

</script>

<style lang="scss" scoped>
    @import '../../style/mixin';

    html,body{
      background-color: #ffffff;
      height:100%;
    }

    .loginContainer{
        p, span, input{
            font-family: Helvetica Neue,Tahoma,Arial;
        }
      background-color: #ffffff;
    }
    .change_login{
        position: absolute;
        @include ct;
        right: 0.75rem;
        @include sc(.7rem, #fff);
    }

    .loginForm{
        background-color: #fff;
        margin-top: 3rem;
        .input_container{
            display: flex;
            justify-content: space-between;
            padding: .6rem .8rem;
            input{
                font-size:0.9rem;
                padding:0.4rem 3% 0.4rem 15%;
                width:100%;
                color: #8492af;
            }
            button{
                position: absolute;
                top: 0.4rem;
                right: 0.8rem;
                color: #37a8eb;
                background: none !important;
                border: 1px solid #37a8eb;
                padding: 0.4rem 0.8rem;
                border-radius: 10rem;
                font-size: 0.9rem;
            }

        }
        .phone_number{
            padding: .5rem 5%;
            position:relative;
            margin-bottom:0.5rem;
        }
        .phone_number:after{
            content:'';
            position:absolute;
            width: 90%;
            height: 1px;
            background: #f1f1f1;
            left: 6%;
            bottom: 0;
        }
        .phone_number-img{
            position:absolute;
            top: 0.8rem;
            left: 1.5rem;
            width: 1.1rem;
        }
        .captcha_code_container{
            height: 2.2rem;
            .img_change_img{
                display: flex;
                align-items: center;
                img{
                    @include wh(3.5rem, 1.5rem);
                    margin-right: .2rem;
                }
                .change_img{
                    display: flex;
                    flex-direction: 'column';
                    flex-wrap: wrap;
                    width: 2rem;
                    justify-content: center;
                    p{
                        @include sc(.55rem, #666);
                    }
                    p:nth-of-type(2){
                        color: #3b95e9;
                        margin-top: .2rem;
                    }
                }
            }
        }
    }
    .login_tips{
        @include sc(.5rem, #999);
        padding: .4rem .6rem;
        line-height: .7rem;
        a{
            color: #3b95e9;
        }
    }
    .login_container{
            font-size: 0.9rem;
            color: #fff;
            background-color: #37a8eb;
            padding: .7rem 0;
            text-align: center;
            width: 60%;
            margin: 0 auto;
            margin-top: 2rem;
            border-radius:10rem;
    }
    .button_switch{
        background-color: #ccc;
        display: flex;
        align-items: center;
        justify-content: space-between;
        @include wh(2rem, .7rem);
        padding: 0 .2rem;
        border: 1px;
        border-radius: 0.5rem;
        position: relative;
        transition: all .3s;
        .circel_button{
            transition: all .3s;
            position: absolute;
            z-index: 1;
            @include wh(1.2rem, 1.2rem);
            left: -.1rem;
            box-shadow: 0 0.026667rem 0.053333rem 0 rgba(0,0,0,.1);
            background-color: #f1f1f1;
            border-radius: 50%;

        }
        .trans_to_right{
            transform: translateX(1.3rem);
        }
        span{
            @include sc(.45rem, #fff);
            transform: translateY(.05rem);
            line-height: .6rem;
        }
        span:nth-of-type(2){
            transform: translateY(-.08rem);
        }
    }
    .change_to_text{
        background-color: #4cd964;
    }
    .to_forget{
        float: right;
        @include sc(.6rem, #3b95e9);
        margin-right: .3rem;
    }

    .login_head{
      padding-top: 4rem;
    }
    .login_head_img {
      width: 60%;
      margin: 0 auto;
      img{
        width:100%;
        display:block;
      }
    }
</style>
